﻿//-----------------------------------------------------------------
//
// @(#)$Id: CustomerResolveBrowse.aspx.cs,v 1.0 2013/12/13 17:54:33 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:54:33 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using ebay.Dal;

public partial class UI_Orders_CustomerResolveBrowse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string orderlineItemId = Request["olId"];
            string ebayuserId = Request["ebId"];
            string productId = Request["pid"];
            string stockId = Request["fsId"];
            string iType = Request["iType"];

            txtfsId.Value = stockId;
            string msg="";
            int val = CheckOrderStatusId(ebayuserId, orderlineItemId, ref msg);
            if (val == 0)
            {
                Response.Write("<script>alert('" + msg + "'); window.close();</script>");
            }
            if (iType == "U")
            {
                Bind(orderlineItemId, ebayuserId, productId);
            }
        }
    }

    void Bind(string OrderLineItemId, string EbayUserId, string ProductId)
    {
        string cmdText = @"select p.UserCode, r.Quantity from CustomerResolve r
            left outer join Product p on r.ProductId = p.ProductId
            where r.OrderLineItemId = @OrderLineItemId
            and r.EbayUserId = @EbayUserId
            and r.ProductId = @ProductId";
        SqlParameter[] p = { 
            new SqlParameter("@OrderLineItemId",OrderLineItemId),
            new SqlParameter("@EbayUserId",EbayUserId),
            new SqlParameter("@ProductId",ProductId)
        };
        DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, p);
        DataRow row = table.Rows[0];
        txtUserCode.Value = row["UserCode"].ToString();
        txtQty.Value = row["Quantity"].ToString();
    }

    int CheckOrderStatusId(string ebayUserId, string orderLineItemId,ref string msg)
    {
        string cmdText = @"select OrderStatusId from Orders where EbayUserId =@EbayUserId and OrderLineItemId =@OrderLineItemId ";
        SqlParameter[] p = { 
            new SqlParameter("@EbayUserId",SqlDbType.VarChar),
            new SqlParameter("@OrderLineItemId",SqlDbType.VarChar)
        };
        p[0].Value = ebayUserId;
        p[1].Value = orderLineItemId;
        try
        {
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, p);
            if (table.Rows.Count > 0)
            {
                DataRow row = table.Rows[0];
                string val = row["OrderStatusId"].ToString();
                if (val == "2")
                {
                    msg = "订单已审核";
                    return 0;
                }
            }
        }
        catch
        {
            msg = "服务器内部错误";
            return 0;
        }
        return 1;
    }

    protected void ButtonSend_Click(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }

        string orderlineItemId = Request["olId"];
        string ebayuserId = Request["ebId"];
        string productId = Request["pid"];
        string iType = Request["iType"];

        string userCode = txtUserCode.Value;
        string cmdText = @"Select ProductId,isnull(c.FOrderPrice,0) as FOrderPrice,ISNULL(m.FStockId,0) as FStockId From Product p
            left outer join StockMapping m on p.DeliveryWarehouseId = m.WareHouseId
            left outer join vw_Cost c on p.FitemId = c.FItemID
            Where UserCode = @UserCode";
        DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, new SqlParameter("@UserCode", userCode));
        if (table.Rows.Count > 1)
        {
            WebUtility.ShowMsg("一个规格不能对应多个产品", this);
            return;
        }
        string newPId = table.Rows[0]["ProductId"].ToString();
        string fsId = table.Rows[0]["FStockId"].ToString();
        decimal fPrice = Convert.ToDecimal(table.Rows[0]["FOrderPrice"]);

        using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
        {
            connection.Open();
            using (SqlTransaction tran = connection.BeginTransaction())
            {
                if (iType == "U")
                {
                    string cmdText2 = "Update CustomerResolve Set Quantity = @Quantity ,ProductId = @ProductId Where EbayUserId = @EbayUserId And OrderLineItemId = @OrderLineItemId And ProductId = @Ori_ProductId";
                    SqlParameter[] p2 = {
                        new SqlParameter("@Quantity",SqlDbType.Int),
                        new SqlParameter("@ProductId",SqlDbType.VarChar),
                        new SqlParameter("@EbayUserId",SqlDbType.VarChar),
                        new SqlParameter("@OrderLineItemId",SqlDbType.VarChar),
                        new SqlParameter("@Ori_ProductId",SqlDbType.VarChar)
                    };
                    p2[0].Value = txtQty.Value;
                    p2[1].Value = newPId;
                    p2[2].Value = ebayuserId;
                    p2[3].Value = orderlineItemId;
                    p2[4].Value = productId;

                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, cmdText2, p2);
                }
                else
                {
                    string cmdText3 = " Insert Into CustomerResolve (EbayUserId,OrderLineItemId,ProductId,CustomerLable,Quantity,SalePrice,ItemCost,TransactionFees,PayFee,TheoryPostAge,ActualPostage,PackageFee,RefundLoss,Rate,WarehouseId,MNote,Muser,Mtime,Auser,Atime)"
                        + "Values (@EbayUserId,@OrderLineItemId,@ProductId,'',@Quantity,@SalePrice,@ItemCost,0,0,0,0,0,0,1,@WarehouseId,'',@Muser,getdate(),'','1900-1-1')";
                    SqlParameter[] p ={
                        new SqlParameter("@EbayUserId",SqlDbType.VarChar),
                        new SqlParameter("@OrderLineItemId",SqlDbType.VarChar),
                        new SqlParameter("@ProductId",SqlDbType.VarChar),
                        new SqlParameter("@Quantity",SqlDbType.Int),
                        new SqlParameter("@SalePrice",SqlDbType.Decimal),
                        new SqlParameter("@ItemCost",SqlDbType.Decimal),
                        new SqlParameter("@WarehouseId",SqlDbType.Int),
                        new SqlParameter("@Muser",SqlDbType.VarChar)
                    };
                    p[0].Value = ebayuserId;
                    p[1].Value = orderlineItemId;
                    p[2].Value = newPId;
                    p[3].Value = txtQty.Value;
                    p[4].Value = fPrice;
                    p[5].Value = fPrice;
                    p[6].Value = fsId;
                    p[7].Value = Session["UserId"].ToString();

                    SqlHelper.ExecuteNonQuery(tran, CommandType.Text, cmdText3, p);
                }

                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter() { ParameterName = "@eBayUserID", SqlDbType = SqlDbType.NVarChar, Size = 80, Value = ebayuserId },
                    new SqlParameter() { ParameterName = "@OrderLineItemID", SqlDbType = SqlDbType.NVarChar, Size = 30, Value = orderlineItemId }
                };
                SqlHelper.ExecuteNonQuery(tran, CommandType.Text, SqlHelper.UpdateProfitCenterString, parameters);

                try
                {
                    tran.Commit();
                    Response.Write("<script>window.returnValue = true;window.close();</script>");
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    WebUtility.ShowMsg("操作失败!", this);
                    Log.WriteLog(ex.Message);
                }
            }
        }

        return;
    }
}
